/*
 * Copyright 2007 by LongTop Corporation.
 * Softpack ChuangXin Building 15F, XiaMen, FuJian, PRC 361005
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 *
 */
package com.longtop.framework.sys.log.web;

import java.util.Date;
import java.util.HashMap;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.sys.log.service.LogService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.DateUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.AbstractControl;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Text;

/**
 * 
 * @author IntelliWeb Team
 * @version 2.0
 * 
 */
public class LogAction extends BaseAction {
	private final static String DATASET_LOG = "Dataset_Log";

	/**
	 * 页面初始化方法。<br>
	 * 该方法可以实现以下功能：<br>
	 * 1、调用数据访问层加载页面Dataset数据<br>
	 * 2、获取页面定义的控件，动态改变控件属性<br>
	 * 3、动态添加、删除页面控件<br>
	 * 4、页面转向：request的forward、 response的sendRedirect等<br>
	 * 
	 * 
	 * @param context
	 */
	public void init(IInitContext context) {
		super.init(context);

	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 获取页面上的dataset的结构
		Dataset logDataset = context.getDataset("Dataset_Log");
		QueryConditions queryConditions = WebUtil.createQueryConditions(); // 查询条件辅助类
		// 调用Spring的业务逻辑类
		LogService logService = getBean(LogService.BEAN_ID);

		// 设置查询条件，默认只查询当天的日志信息
		Date today = DateUtil.getDate();
		Date tomorrow = DateUtil.addDay(today, 1);

		// 查询的记录数，由dataset的定义决定
		int pageSize = logDataset.getPageSize();

		queryConditions.setCondition("logTime", QueryConditions.GREATER_EQUAL, today);
		queryConditions.setCondition("logTime", QueryConditions.LESS_THAN, tomorrow);

		QueryResult queryResult = logService.loadLog(queryConditions, 1, pageSize);
		BeanUtil.queryResultToDataset(queryResult, logDataset, false, true);

		// 把当前时间赋给界面控件，传回客户端显示
		AbstractControl Editor_logTime1 = context.getControl("Text_logTime1");
		AbstractControl Editor_logTime2 = context.getControl("Text_logTime2");

		String strToday = DateUtil.formatDate(today, "yyyyMMdd");
		log.logDebug("strToday:" + strToday);
		if (Editor_logTime1 != null) {
			((Text) Editor_logTime1).setValue(strToday);
		}

		if (Editor_logTime2 != null) {
			((Text) Editor_logTime2).setValue(strToday);
		}
	}

	public void searchLog(IActionContext context) {
		commonQuery(context, true);

	}

	/**
	 * 执行翻页查询
	 * 
	 * @param context
	 */
	public void loadLog(IActionContext context) {

		// 调用公用方法
		commonQuery(context, false);
	}

	/**
	 * 每个业务模块公共查询方法，供自定义查询方法及beforeLoadData调用
	 * 
	 * @param context
	 *            intelliWeb Web上下文
	 * @param customAction
	 *            是否自定义方法
	 */
	private void commonQuery(IActionContext context, boolean ispage1) {
		// 获取页面上的dataset的结构
		int pageNo = 1; // 当前第几页
		HashMap hashMap = new HashMap(); // ibatis查询的参数条件
		QueryConditions queryConditions = WebUtil.createQueryConditions(); // 查询条件辅助类
		LogService logService = getBean(LogService.BEAN_ID);
		// 获取页面上的dataset的结构
		Dataset logDataset = context.getDataset(DATASET_LOG);

		// 验证数据集对象是否存在
		if (!WebUtil.validateDatasetExist(this, logDataset, DATASET_LOG)) {
			return;
		}

		if (logDataset == null)
			return;
		// 取得界面传入的查询条件(必须设置command的submitForm=true)
		String bizType = context.getElementValue("Text_BizType");
		String logTime1 = context.getElementValue("Text_logTime1");
		String logTime2 = context.getElementValue("Text_logTime2");
		log.logDebug("logTime1:" + logTime1 + "    logTime2:" + logTime2);
		String ipAddr = context.getElementValue("Text_IpAddr");
		String logInfo = context.getElementValue("Text_LogInfo");
		Date logTimeDate1 = null;
		if (logTime1 != null && !logTime1.equals("")) {
			logTimeDate1 = DateUtil.parseDate2(logTime1);
		}
		Date logTimeDate2 = null;
		// logTime2加1天
		if (logTime2 != null && !logTime2.equals("")) {
			logTimeDate2 = DateUtil.addDay(DateUtil.parseDate2(logTime2), 1);
		}
		log.logDebug("logTimeDate1:" + (logTimeDate1 == null ? "" : logTimeDate1.toString()));
		log.logDebug("logTimeDate2:" + (logTimeDate2 == null ? "" : logTimeDate2.toString()));

		queryConditions.setCondition("moduleId", QueryConditions.EQUAL, bizType);
		queryConditions.setCondition("ipAddr", QueryConditions.EQUAL, ipAddr);
		queryConditions.setCondition("logTime", QueryConditions.GREATER_EQUAL, logTimeDate1);
		queryConditions.setCondition("logTime", QueryConditions.LESS_EQUAL, logTimeDate2);
		queryConditions.setCondition("logInfo", QueryConditions.INCLUDE, logInfo);

		// 查询的记录数，由dataset的定义决定
		int pageSize = logDataset.getPageSize();
		// 是否第一页
		if (ispage1) {
			pageNo = 1;
		} else {
			pageNo = logDataset.getPageNo();

		}

		// 调用service层，依据条件查询第一页记录，并获取总页数
		QueryResult queryResult = logService.loadLog(queryConditions, pageNo, pageSize);
		BeanUtil.queryResultToDataset(queryResult, logDataset, false, true);
	}

}